package com.library.framework.manager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.library.books.domain.Increment;
import com.library.books.mapper.IncrementMapper;
import com.library.books.service.IIncrementService;
import com.library.common.enums.IncrementStatus;
import com.library.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

/**
 * 确保应用退出时能关闭后台线程
 *
 * @author hb
 */
@Component
public class ShutdownManager
{
    private static final Logger logger = LoggerFactory.getLogger("sys-user");

    @Autowired
    private IIncrementService incrementService;

    @PreDestroy
    public void destroy()
    {
        updateUnuse();
        shutdownAsyncManager();
    }

    /**
     * 停止异步执行任务
     */
    private void shutdownAsyncManager()
    {
        try
        {
            logger.info("====关闭后台任务任务线程池====");
            AsyncManager.me().shutdown();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage(), e);
        }
    }

    /** 所有cntype允许使用 */
    private void updateUnuse() {
        logger.info("====释放所有索引号分类====");
        incrementService.update(Wrappers.lambdaUpdate(Increment.class)
            .set(Increment::getUse, IncrementStatus.UNUSE.getCode())
        );
    }

    /** 将所有门给断连 */
    private void doorsDown() {
        logger.info("====智能门禁断连====");
    }
}
